from TCPmodbus_wrapper import ModbusMaster
import time

def test_all_functions():
    master = ModbusMaster()
    unit_id = 1
    
    print("=== Testing all Modbus functions ===")
    
    # 测试读取功能
    print("\n1. Testing read functions:")
    print("Reading coils (address 0-7):")
    print(master.read_coils(unit_id, 0, 8))
    
    print("\nReading discrete inputs (address 0-7):")
    print(master.read_discrete_inputs(unit_id, 0, 8))
    
    print("\nReading holding registers (address 0-4):")
    print(master.read_holding_registers(unit_id, 0, 5))
    
    print("\nReading input registers (address 0-4):")
    print(master.read_input_registers(unit_id, 0, 5))
    
    # 测试写入功能
    print("\n2. Testing write functions:")
    print("Writing single coil (address 5, ON)")
    master.write_single_coil(unit_id, 5, True)
    
    print("\nWriting single register (address 10, value 1234)")
    master.write_single_register(unit_id, 10, 1234)
    
    print("\nWriting multiple coils (address 20-27, alternating ON/OFF)")
    master.write_multiple_coils(unit_id, 20, [True, False, True, False, True, False, True, False])
    
    print("\nWriting multiple registers (address 30-32, values [111, 222, 333])")
    master.write_multiple_registers(unit_id, 30, [111, 222, 333])
    
    # 验证写入结果
    print("\n3. Verifying writes:")
    print("Reading coil 5:")
    print(master.read_coils(unit_id, 5, 1))
    
    print("\nReading register 10:")
    print(master.read_holding_registers(unit_id, 10, 1))
    
    print("\nReading coils 20-27:")
    print(master.read_coils(unit_id, 20, 8))
    
    print("\nReading registers 30-32:")
    print(master.read_holding_registers(unit_id, 30, 3))

if __name__ == "__main__":
    while True:
        test_all_functions()